/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "system.h"
#include "adc.h"
#include "protocol.h"
#include "frame_driver.h"
#include "data.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId nTaskIdleHandle;
osThreadId nTaskReceiveHandle;
osThreadId nTaskTransferHandle;
osMessageQId nQueueReceiveHandle;
osMessageQId nQueueTransferHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void TaskIdle(void const * argument);
void TaskReceive(void const * argument);
void TaskTransfer(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* Hook prototypes */
void configureTimerForRunTimeStats(void);
unsigned long getRunTimeCounterValue(void);

/* USER CODE BEGIN 1 */
#define dTxBufferSize 520
volatile uint32_t CPU_RunTime = 0;
uint8_t TxBuffer[dTxBufferSize] = {0};

/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{
  CPU_RunTime = 0;
}

__weak unsigned long getRunTimeCounterValue(void)
{
  return CPU_RunTime;
}
/* USER CODE END 1 */

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* definition and creation of nQueueReceive */
  osMessageQDef(nQueueReceive, 520, uint8_t);
  nQueueReceiveHandle = osMessageCreate(osMessageQ(nQueueReceive), NULL);

  /* definition and creation of nQueueTransfer */
  osMessageQDef(nQueueTransfer, 520, uint8_t);
  nQueueTransferHandle = osMessageCreate(osMessageQ(nQueueTransfer), NULL);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of nTaskIdle */
  osThreadDef(nTaskIdle, TaskIdle, osPriorityNormal, 0, 256);
  nTaskIdleHandle = osThreadCreate(osThread(nTaskIdle), NULL);

  /* definition and creation of nTaskReceive */
  osThreadDef(nTaskReceive, TaskReceive, osPriorityAboveNormal, 0, 256);
  nTaskReceiveHandle = osThreadCreate(osThread(nTaskReceive), NULL);

  /* definition and creation of nTaskTransfer */
  osThreadDef(nTaskTransfer, TaskTransfer, osPriorityAboveNormal, 0, 128);
  nTaskTransferHandle = osThreadCreate(osThread(nTaskTransfer), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_TaskIdle */
/**
  * @brief  Function implementing the nTaskIdle thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_TaskIdle */
void TaskIdle(void const * argument)
{
  /* USER CODE BEGIN TaskIdle */
  /* Infinite loop */
  System_Init();
  for(;;)
  {
    osDelay(150);
    Data_DeviceSample();
    //Common_FreeRTOS_CPU_Usage_Display();
    //dLOG("run..\n");
  }
  /* USER CODE END TaskIdle */
}

/* USER CODE BEGIN Header_TaskReceive */
/**
* @brief Function implementing the nTaskReceive thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_TaskReceive */
void TaskReceive(void const * argument)
{
  /* USER CODE BEGIN TaskReceive */
  /* Infinite loop */
  BaseType_t xReturn = pdTRUE;
  uint8_t data = 0;
  frame_handler frame;
  if(frame_init(&frame,1,dFrame_LengthMax) < 0){while(1)osDelay(1);}
  frame_identify_add(&frame,dFrame_IdentifyA);
  System_ReadyWait();
  for(;;)
  {
    xReturn = xQueueReceive(nQueueReceiveHandle,(void*)(&data),portMAX_DELAY);
    if(xReturn != pdTRUE)continue;
    if(frame_find(&frame,data) != 0)continue;
    //frame_data_printf(frame.data_rx);
    Protocol_FrameAnalysis(frame.rx.pbuf,frame.data_rx.size + dFrame_FormatSize(frame.data_rx.identify),frame.data_rx.pbuf,frame.data_rx.size);
  }
  /* USER CODE END TaskReceive */
}

/* USER CODE BEGIN Header_TaskTransfer */
/**
* @brief Function implementing the nTaskTransfer thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_TaskTransfer */
void TaskTransfer(void const * argument)
{
  /* USER CODE BEGIN TaskTransfer */
  /* Infinite loop */
  BaseType_t xReturn = pdTRUE;
  uint16_t pos = 0;
  System_ReadyWait();
  for(;;)
  {
    pos = 0;
    xReturn = xQueueReceive(nQueueTransferHandle,(void*)(&TxBuffer[pos++]),portMAX_DELAY);
    if(xReturn != pdTRUE){continue;}
    while((xReturn == pdTRUE) && (pos < dTxBufferSize)){xReturn = xQueueReceive(nQueueTransferHandle,(void*)(&TxBuffer[pos++]),0);}
    if(xReturn != pdTRUE)pos--;
    Protocol_DataTransmit(false,TxBuffer,pos);
  }
  /* USER CODE END TaskTransfer */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

